package com.tanhua.recommend.listener;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementScore;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoScore;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RecommendListener {

    @Autowired
    private MongoTemplate mongoTemplate;

     @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "tanhua.movement.recommend",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.movement"
    ))
    public void movementListener(String message) {
        System.out.println("处理动态消息");
        try {
            //1、获取数据
            JSONObject jsonObject = JSON.parseObject(message);

            Long userId = jsonObject.getLong("userId");
            String type = jsonObject.getString("type");
            String logTime = jsonObject.getString("logTime");
            String busId = jsonObject.getString("busId");

            //2、保存数据
            Movement movement = mongoTemplate.findById(new ObjectId(busId), Movement.class);
            if (movement != null) {
                MovementScore movementScore = new MovementScore();
                movementScore.setUserId(userId);
                movementScore.setDate(System.currentTimeMillis());
                movementScore.setMovementId(movement.getPid());
                movementScore.setScore(getMovementScore(type, movement));

                mongoTemplate.save(movementScore);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Double getMovementScore(String type,Movement movement) {
        //0201为发动态  基础5分 一张图片1分 50以内1分，50~100之间2分，100以上3分
        //0202为浏览动态， 1
        //0203为动态点赞， 5
        //0204为动态喜欢， 8
        //0205为评论，     10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢   -8
        Double score = 0d;
        switch (type) {
            case "0201":
                score = 5d;
                score += movement.getMedias().size();
                int length = StrUtil.length(movement.getTextContent());
                if (length >= 0 && length < 50) {
                    score += 1;
                } else if (length < 100) {
                    score += 2;
                } else {
                    score += 3;
                }
                break;
            case "0202":
                score = 1d;
                break;
            case "0203":
                score = 5d;
                break;
            case "0204":
                score = 8d;
                break;
            case "0205":
                score = 10d;
                break;
            case "0206":
                score = -5d;
                break;
            case "0207":
                score = -8d;
                break;
            default:
                break;
        }
        return score;
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "tanhua.video.recommend",
                    durable = "true"
            ),
            exchange = @Exchange(
                    value = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"log.video"})
    )
    public void videoListener(String message) {
        System.out.println("处理视频消息");
        try {
            //1、获取数据
            JSONObject jsonObject = JSON.parseObject(message);

            Long userId = jsonObject.getLong("userId");
            String type = jsonObject.getString("type");
            String logTime = jsonObject.getString("logTime");
            String busId = jsonObject.getString("busId");

            //保存数据
            Video video = mongoTemplate.findById(new ObjectId(busId), Video.class);
            if (video != null) {
                VideoScore videoScore = new VideoScore();
                videoScore.setUserId(userId);
                videoScore.setVideoId(video.getVid());
                videoScore.setDate(System.currentTimeMillis());
                videoScore.setScore(getVideoScore(type));

                mongoTemplate.save(videoScore);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Double getVideoScore(String type) {
        //0301为发小视频，0302为小视频点赞，0303为小视频取消点赞，0304为小视频评论
        Double score = 0d;
        switch (type) {
            case "0301":
                score = 2d;
                break;
            case "0302":
                score = 5d;
                break;
            case "0303":
                score = -5d;
                break;
            case "0304":
                score = 10d;
                break;
            default:
                break;
        }
        return score;
    }
}